﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using MVCAddressBook.Models.Entities;
using MVCAddressBook.Models.Interfaces;
using System.Data.SqlClient;
using System.Data;

namespace MVCAddressBook.Models.Models
{
    public class AdminService:IAdminService
    {

        public bool RegUser(AdminInfo admininfo)
        {
            string sql = "insert into [Admin]([Name],[Password],[LastActiveIP],[IsLock],[RegIP],[RegTime]) values(@Name,@Password,@LastActiveIP,@IsLock,@RegIP,@RegTime)";
            SqlParameter[] param = new SqlParameter[] { 
                new SqlParameter("@Name", SqlDbType.NVarChar, 50),
                new SqlParameter("@Password", SqlDbType.VarChar, 50),
                new SqlParameter("@LastActiveIP", SqlDbType.VarChar, 50),
                new SqlParameter("@IsLock", SqlDbType.Int),
                new SqlParameter("@RegIP", SqlDbType.VarChar, 50),
                new SqlParameter("@RegTime", SqlDbType.DateTime),
            };
            param[0].Value = admininfo.Name;
            param[1].Value = admininfo.Password;
            param[2].Value = admininfo.LastActiveIP;
            param[3].Value = admininfo.IsLock;
            param[4].Value = admininfo.RegIP;
            param[5].Value = admininfo.RegTime;
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sql, param) > 0;
        }

        public bool Deluser(string Name)
        {
            string sql = "delete from [Admin] where [Name]=@Name";
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@Name", SqlDbType.NVarChar,50) };
            param[0].Value = Name;
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, sql, param) > 0;
        }

        public bool AlterAdminPassword(string Name, string NewPassword)
        {
            string strsql = "update [Admin] set [Password]=@NewPassword where [Name]=@Name";
            SqlParameter[] param = new SqlParameter[] {
                new SqlParameter("@NewPassword", SqlDbType.VarChar, 50),
                new SqlParameter("@Name", SqlDbType.NVarChar, 50) };
            param[0].Value = NewPassword;
            param[1].Value = Name;
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, strsql, param) > 0;
        }

        public bool IsExistUser(string Name)
        {
            string strsql = "select count(*) from [Admin] where [Name]=@Name";
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@Name", SqlDbType.NVarChar, 50) };
            param[0].Value = Name;
            return (int)SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, strsql, param) > 0;
        }


        public bool EditUser(AdminInfo admininfo)
        {
            string strsql = "update [Admin] set [Name]=@Name,[Password]=@Password,[LastActiveIP]=@LastActiveIP,[IsLock]=@IsLock,[RegIP]=@RegIP,[RegTime]=@RegTime where [ID]=@ID";
            SqlParameter[] param = new SqlParameter[] {
                new SqlParameter("@Name", SqlDbType.NVarChar, 50),
                new SqlParameter("@Password", SqlDbType.VarChar, 50),
                new SqlParameter("@LastActiveIP", SqlDbType.VarChar, 50),
                new SqlParameter("@IsLock", SqlDbType.Int),
                new SqlParameter("@RegIP", SqlDbType.VarChar, 50),
                new SqlParameter("@RegTime", SqlDbType.DateTime),
                new SqlParameter("@ID", SqlDbType.Int) 
            };

            param[0].Value = admininfo.Name;
            param[1].Value = admininfo.Password;
            param[2].Value = admininfo.LastActiveIP;
            param[3].Value = admininfo.IsLock;
            param[4].Value = admininfo.RegIP;
            param[5].Value = admininfo.RegTime;
            param[6].Value = admininfo.ID;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, strsql, param) > 0;
        }


        public IList<AdminInfo> GetAdminList()
        {
            IList<AdminInfo> list = new List<AdminInfo>();

            string strsql = "SELECT ID, Name, Password, IsLock, RegTime, LastActiveIP, RegIP FROM Admin ORDER BY ID DESC ";
            
            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, strsql, null))
            {
                while (rdr.Read())
                {
                    list.Add(new AdminInfo
                    {
                        ID = rdr.GetInt32(0),
                        Name = rdr.GetString(1),
                        Password = rdr.GetString(2),
                        IsLock = rdr.GetInt32(3),
                        RegTime = rdr.GetDateTime(4),
                        LastActiveIP = rdr.GetString(5),
                        RegIP = rdr.GetString(6),
                    });
                }
            }
            return list;
        }

        public AdminInfo GetAdmin(int ID)
        {
            string strsql = "SELECT ID, Name, Password, IsLock, RegTime, LastActiveIP, RegIP FROM Admin where ID=@ID";
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int) };
            param[0].Value = ID;
            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, strsql, param))
            {
                if (rdr.Read())
                {
                    return new AdminInfo
                    {
                        ID = rdr.GetInt32(0),
                        Name = rdr.GetString(1),
                        Password = rdr.GetString(2),
                        IsLock = rdr.GetInt32(3),
                        RegTime = rdr.GetDateTime(4),
                        LastActiveIP = rdr.GetString(5),
                        RegIP = rdr.GetString(6),

                    };
                }
                else
                {
                    return new AdminInfo();
                }
            }
        }

        public AdminInfo GetAdmin(string Name)
        {
            string strsql = "SELECT ID, Name, Password, IsLock, RegTime, LastActiveIP, RegIP FROM Admin where Name=@Name";
            SqlParameter[] param = new SqlParameter[] { new SqlParameter("@Name", SqlDbType.NVarChar) };
            param[0].Value = Name;
            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, strsql, param))
            {
                if (rdr.Read())
                {
                    return new AdminInfo
                    {
                        ID = rdr.GetInt32(0),
                        Name = rdr.GetString(1),
                        Password=rdr.GetString(2),
                        IsLock=rdr.GetInt32(3),
                        RegTime=rdr.GetDateTime(4),
                        LastActiveIP=rdr.GetString(5),
                        RegIP=rdr.GetString(6),
                        
                    };
                }
                else
                {
                    return new AdminInfo();
                }
            }
        }

        public bool IsLogin(string Name, string Password)
        {
            string sql = "select count(*) from [Admin] where [Name]=@Name and [Password]=@Password";
            SqlParameter[] param = new SqlParameter[] {
                new SqlParameter("@Name", SqlDbType.NVarChar, 50),
                new SqlParameter("@Password", SqlDbType.VarChar, 50) 
            };
            param[0].Value = Name;
            param[1].Value = Password;
            return (int)SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, sql, param) > 0;
        }


        public bool ChangePassword(string Name, string OldPassword, string NewPassword)
        {
            AdminInfo user = GetAdmin(Name);
            if (user == null || user.Password != OldPassword)
            {
                return false;
            }
            else
            {
                return AlterAdminPassword(Name, NewPassword);
            }
        }
    }
}